﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringDefs.Security
{
    public static class Security
    {

        /// <summary>
        /// Generates random passwords of specified length using PRNs
        /// </summary>
        /// <param name="length">The intended length of the randomly generated passwords</param>
        /// <returns></returns>
        public static string GenPass(this int length)
        {
            Random r1 = new Random();//Whether it will be the alphabet or number or symbol [0-1]
            Random r2 = new Random();//Which Alphabet
            Random r3 = new Random();//Will it be upper case or not
            Random r4 = new Random();//Which Symbol
            Random r5 = new Random();//Which Number
            char[] alphas = new char[26];
            char[] numbers = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
            char[] symbols = {'`','~','!','@','#','$','%','^','&','*','(',')'
                ,'-','_','=','+','{','}','[',']','|','\\',':',';','"','\'','<','>',',','.','?','/'};

            char c = 'a';
            for (int i = 0; i < 26; i++, c++)
                alphas[i] = c;

            char[] pass = new char[length];
            for (int i = 0; i < length; i++)
            {

                int x = r1.Next(3);
                int y = r2.Next(26);
                int z = r3.Next(2);
                int w = r4.Next(symbols.Length);
                int s = r5.Next(numbers.Length);
                if (x == 0)
                {
                    if (z == 0)
                        pass[i] = alphas[y];
                    else
                        pass[i] = Convert.ToChar(alphas[y].ToString().ToUpper());
                }
                if (x == 1)//Number
                {
                    pass[i] = numbers[s];
                }
                if (x == 2)//Symbol
                {
                    pass[i] = symbols[w];
                }
            }
            return new string(pass);
        }
 
    
    }
}
